package ding.mapsample;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.Log;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem>{
	private ArrayList<OverlayItem> myoverlays = new ArrayList<OverlayItem>();
	private Context mycontext=null;
	
	public MyItemizedOverlay(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
		// TODO Auto-generated constructor stub
	}
	
	public MyItemizedOverlay(Drawable defaultMarker,Context context) {
		super(boundCenterBottom(defaultMarker));
		// TODO Auto-generated constructor stub
		mycontext = context;
	}

	@Override
	protected OverlayItem createItem(int i) {
		// TODO Auto-generated method stub
		Log.d("mydebug", "createItem:"+i);
		return myoverlays.get(i);
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		Log.d("mydebug", "myoverlays size:"+myoverlays.size());
		return myoverlays.size();
	}
	
	public void addOverlay(OverlayItem overlay){
		//myoverlays.add(overlay);
		myoverlays.add(overlay);
	    populate();
	}
	
	
	public void UpdateOverlay(OverlayItem overlay){
		myoverlays.add(0, overlay);
		
	    populate();
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		// TODO Auto-generated method stub
		Log.d("mydebug", "run overlaydraw");
		super.draw(canvas, mapView, shadow);
	}

	@Override
	protected boolean onTap(int index) {
		// TODO Auto-generated method stub
		OverlayItem item = myoverlays.get(index);
		AlertDialog.Builder dialog = new AlertDialog.Builder(mycontext);
		dialog.setTitle(item.getTitle());
		dialog.setMessage(item.getSnippet());
		dialog.show();
		return true;
	}
	
	

}
